<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Lazy Load</title>
</head>

<body>

  <div class="placeholder"></div>
  <div class="container">
    <h1>Lazy Load</h1>
    <p>懒加载</p>
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
    <img alt="" class="image"
      data-src="https://img0.baidu.com/it/u=3240022098,100400902&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500">
  </div>

  <!-- <script>
    function throttle(func, delay) {
      let timer = null;
      return function () {
        if (!timer) {
          timer = setTimeout(() => {
            func();
            timer = null;
          }, delay);
        }
      }
    }

    function getOffsetTopFromRoot(ele) {
      let top = ele.offsetTop;
      while (ele = ele.offsetParent) {
        top += ele.offsetTop;
      }
      return top;
    }

    const images = document.querySelectorAll('img');

    const loadImage = (img) => img.src ||= img.dataset.src;

    function lazyLoadImage(img) {
      const clientHeight = document.documentElement.clientHeight;
      const scrollTop = document.documentElement.scrollTop;
      // if (img.getBoundingClientRect().top <= clientHeight) {
      if (getOffsetTopFromRoot(img) <= clientHeight + scrollTop) {
        loadImage(img);
      }
    }

    const loadImages = throttle(() => {
      images.forEach(img => {
        lazyLoadImage(img);
      })
    }, 500);

    loadImages();
    window.addEventListener('scroll', loadImages);

  </script> -->

  <script>
    const images = document.querySelectorAll('img');

    const loadImage = (img) => {
      img.src ||= img.dataset.src;
      observer.unobserve(img);
    }
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.intersectionRatio > 0) {
          loadImage(entry.target);
        }
      })
    });

    images.forEach(img => {
      observer.observe(img);
    })
  </script>

  <style>
    .image {
      width: 100%;
      height: 500px;
    }

    .placeholder {
      height: 500px;
      width: 100%;
    }

    .container {
      position: relative;
    }

  </style>
</body>

</html>
